package com.siwei.mes.service.sync.erp.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.util.NullUtils;
import com.siwei.mes.entity.erp.RwdextraInfo;
import com.siwei.mes.entity.sync.erp.ErpCustomer;
import com.siwei.mes.entity.sync.erp.ErpProject;
import com.siwei.mes.mapper.erp.RwdextraMapper;
import com.siwei.mes.mapper.syncErp.ErpProjectMapper;
import com.siwei.mes.service.sync.erp.ErpProjectService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 工程信息(ErpProject)表服务实现类
 *
 * @author linzi
 * @since 2024-07-25 16:52:16
 */
@Service
public class ErpProjectServiceImpl implements ErpProjectService {
    @Resource
    private ErpProjectMapper erpProjectMapper;
    @Resource
    private RwdextraMapper rwdextraMapper;

    @Override
    public ErpProject update(ErpProject entity) {
        ErpProject erpProject = erpProjectMapper.loadById(entity.getId());
        assertTrue(erpProject == null, "工程信息信息不存在");
        entity.setUpdateTime(new Date());
        entity.setIsUpdate(1);//标记为已修改
        erpProjectMapper.updateByPrimaryKeySelective(entity);
        return erpProjectMapper.loadById(entity.getId());
    }

    @Override
    public void delete(Long id) {
        ErpProject erpProject = erpProjectMapper.selectByPrimaryKey(id);
        assertTrue(erpProject == null, "工程信息信息不存在");
        erpProjectMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<ErpProject> getPage(PageQuery<ErpProject, ErpProject> query) {
        ErpProject searchParam = query.getParams();
        PageInfo<ErpProject> pageInfo = PageHelper.startPage(query.getPageNum(), query.getPageSize())
                .doSelectPageInfo(() -> erpProjectMapper.getList(searchParam));
        for (ErpProject erpProject : pageInfo.getList()) {
            erpProject.setContractName(NullUtils.unescapeJava(erpProject.getContractName()));
        }
        return pageInfo;
    }

    @Override
    public ErpProject loadById(Long id) {
        ErpProject erpProject = erpProjectMapper.loadById(id);
        String projectName = erpProject.getProjectName();
        if (StringUtils.isNotBlank(projectName)) {
            erpProject.setExperimentCount(erpProjectMapper.experimentCount(projectName, null));
            erpProject.setExperimentExpCount(erpProjectMapper.experimentCount(projectName, 2));
            erpProject.setTaskListNum(rwdextraMapper.countTaskCount(projectName));
            erpProject.setTotalVolume(rwdextraMapper.countQuantity(projectName));
        }
        return erpProject;
    }

    @Override
    public List<ErpProject> getAll(ErpProject entity) {
        return erpProjectMapper.getList(entity);
    }

    @Override
    public ErpProject saveOrUpdateErp(ErpCustomer erpCustomer, RwdextraInfo rwdextraInfo) {
        Long erpCustomerId = erpCustomer.getId();//品控erpCustomer表的id

        String fHtbh = rwdextraInfo.getFHtbh();//合同编号
        String fGcmc = rwdextraInfo.getFGcmc();//工程名称
        String fGcdz = rwdextraInfo.getFGcdz();//工程地址

        /**判断这个客户下 是否有这个合同编号和工程名称的*/
        ErpProject erpProject = erpProjectMapper.selectIsExist(erpCustomerId, fHtbh, fGcmc);
        if (erpProject == null) {
            erpProject = new ErpProject();
            erpProject.setId(null);
            erpProject.setErpCustomerId(erpCustomerId);
            erpProject.setContractNo(fHtbh);
            erpProject.setContractName(erpCustomer.getCustomerName());
            erpProject.setSgdw(erpCustomer.getCustomerName());
            erpProject.setProjectName(fGcmc);
            erpProject.setLaboratoryProjectName(fGcmc);
            erpProject.setProjectAddress(fGcdz);
            erpProject.setSignDate(new Date());
            erpProject.setCreateTime(new Date());
            erpProject.setUpdateTime(new Date());
            erpProject.setContractType(1);
            erpProject.setIsDel(0);
            erpProject.setIsUpdate(0);
            erpProject.setIsIronMark(0);
            erpProjectMapper.insertSelective(erpProject);
        } else {
            erpProject.setErpCustomerId(erpCustomerId);
            erpProject.setContractNo(fHtbh);
            erpProject.setContractName(erpCustomer.getCustomerName());
            erpProject.setSgdw(erpCustomer.getCustomerName());
            erpProject.setProjectName(fGcmc);
            erpProject.setLaboratoryProjectName(fGcmc);
            erpProject.setProjectAddress(fGcdz);
            erpProjectMapper.updateByPrimaryKeySelective(erpProject);
        }
        return erpProject;
    }

    @Override
    public ErpProject configErpProject(ErpProject entity) {
        ErpProject erpProject = erpProjectMapper.loadById(entity.getId());
        assertTrue(erpProject == null, "工程信息信息不存在");
        entity.setUpdateTime(new Date());
        entity.setIsUpdate(1);//标记为已修改
        erpProjectMapper.updateByPrimaryKeySelective(entity);
        return erpProjectMapper.loadById(entity.getId());
    }

    @Override
    public List<String> getIsIronMarkProjectName() {
        return erpProjectMapper.getIsIronMarkProjectName();
    }
}
